programming4us
           
 
 
Programming

Programming Windows Azure : Table Operations - Updating Entities

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/21/2010 11:37:36 AM

It wouldn’t be much fun if you couldn’t update data once you put it in storage, would it? Thankfully, updating entities in Azure tables is pretty simple once you get the hang of the various merge/conflict options available.

Earlier, you saw how every query result returns with an ETag. This ETag corresponds to a specific version of the entity. If the entity changes, the service will ensure that it gets a new ETag. Updating an entity now becomes a simple matter of retrieving entities, making changes on the client side, and telling the server about your updates and what the ETag was when you first retrieved the entity to detect conflicts. This is similar to concurrency controls in databases.

In .NET code, this is tightly integrated with the DataServiceContext. You have several “merge” options from which you can choose, as shown in Table 1.

Table 1. Merge options
Merge optionDescription
AppendOnlyClient changes for new entities are accepted, but changes to existing entities are not. This is the default value where the entity isn’t loaded from the server if it is present in the client cache.
OverwriteChangesAlways update with values from the server, overwriting any client changes.
PreserveChangesClient-side values that have changed are preserved, but other values are updated from the server. Also, when an entity instance exists on the client, it is not loaded from the server. No client-side changes are lost. When an object is updated, the ETag is updated as well, so this catches any errors if changes have occurred on the server without the client’s knowledge.
NoTrackingThere is no client-side tracking. Values are always read from the storage source, and any local changes are overwritten.

In code, to update an entity, you typically follow these steps:

  1. Create a DataServiceContext object, and set its merge option to one of the selections from Table 10-7. You could choose to leave the default value (AppendOnly), because this works well in normal scenarios.

  2. Query for the entity you want to update.

  3. Update the object representation of your entity in code.

  4. Call DataServiceContext.UpdateObject on the objects you updated.

  5. Call DataServiceContext.SaveChanges to push changes back to the server.

Example 1 walks through a simple query-update-save pattern.

Example 1. Simple update
CloudStorageAccount.Parse(ConfigurationSettings.AppSettings
["DataConnectionString"]);
var svc = new TestDataServiceContext
(account.TableEndpoint.ToString(), account.Credentials);
svc.MergeOption = MergeOption.PreserveChanges;
var query = from contact in svc.CreateQuery<Contact>("ContactTable")
where contact.Name == "Steve Jobs"
select contact;
var foundContact = query.FirstOrDefault<Contact>();
foundContact.Address = "One Infinite Loop, Cupertino, CA 95014";
svc.UpdateObject(foundContact);

svc.SaveChanges();


The following HTTP traffic when SaveChanges is called shows this ETag matching mechanism clearly. You already saw how querying for an item sends down an ETag, which is the server’s version of the object. When SaveChanges is called, you send out an update that says, “Update if it matches the earlier ETag.” If successful, the server updates the entity and sends down a new ETag.

HTTP Request (only headers shown)

MERGE /ContactTable(PartitionKey='a844fa27-7ae2-4894-9cc6-dd0dbdcd5ec4',
RowKey='') HTTP/1.1
User-Agent: Microsoft ADO.NET Data Services
x-ms-date: Tue, 21 Apr 2009 16:57:29 GMT
Authorization: SharedKeyLite sriramk:HVNqyS/X/AhqFxhnRKSe0SXTDSzJssFyk9JiyQvwnO4=
Accept: application/atom+xml,application/xml
Accept-Charset: UTF-8
DataServiceVersion: 1.0;NetFx
MaxDataServiceVersion: 1.0;NetFx
Content-Type: application/atom+xml
If-Match: W/"datetime'2009-04-21T06%3A38%3A28.242Z'"
Host: sriramk.table.core.windows.net
Content-Length: 864

HTTP Response
HTTP/1.1 204 No Content
Cache-Control: no-cache
Content-Length: 0
ETag: W/"datetime'2009-04-21T16%3A56%3A29.717Z'"
Server: Table Service Version 1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: 05a9f45f-bab9-417a-89b6-fc7759f31b2f
Date: Tue, 21 Apr 2009 16:56:35 GMT

Other -----------------
- Programming Windows Azure : Table Operations - Understanding Pagination
- Programming Windows Azure : Table Operations - Using Partitioning
- Programming Windows Azure : Table Operations - Querying Data
- Programming Windows Azure : Table Operations - Creating Entities
- Programming Windows Azure : Table Operations - Creating Tables
- iPad Development : Document Management (part 2)
- iPad Development : Document Management (part 1)
- iPad Development : The Split View Concept
- jQuery 1.3 : Developing plugins - Adding new shortcut methods
- jQuery 1.3 : Developing plugins - DOM traversal methods
- Using Cloud Services : Exploring Online Planning and Task Management
- Using Cloud Services : Exploring Online Scheduling Applications
- Using Cloud Services : Exploring Online Calendar Applications
- SOA with .NET and Windows Azure : Service Contracts with WCF (part 3)
- SOA with .NET and Windows Azure : Service Contracts with WCF (part 2)
- SOA with .NET and Windows Azure : Service Contracts with WCF (part 1)
- Cloud Security and Privacy : Data Security and Storage
- iPad SDK : Working with Documents - Desktop Synchronization
- Required Project Images for iPad Apps
- iPhone SDK : GameKit Voice Chat
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us